<template>
    <div>
        <Table border :columns="columns" :data="data">
            <template slot-scope="{ row }" slot="action">
                <div class="action-button"></div>
                <Button v-if="row.dept_audit_status==='draft'" type="primary" size="small" style="margin-right: 5px" @click="openModal(row.id,true,'提报', row.dept_audit_status)">提报</Button>
                <Button v-if="row.dept_audit_status==='commit'" type="primary" size="small" style="margin-right: 5px" @click="openModal(row.id,true,'审批', row.dept_audit_status)">审批</Button>
                <Button v-if="row.dept_audit_status==='audited'&&row.leader_audit_status==='unaudited'" type="primary" size="small" style="margin-right: 5px" @click="openModal(row.id,true,'审批', row.dept_audit_status)">审批</Button>
                <Button v-if="row.dept_audit_status==='commit'" type="error" size="small" style="margin-right: 5px" @click="openModal(row.id,false,'驳回', row.dept_audit_status)">驳回</Button>
                <Button type="success" size="small" @click="toDetail(row.imp_date)">详情</Button>
            </template>
        </Table>
        <Page :total="pageInfo.total" :current="pageInfo.pageNo" @on-change="changePage" />
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="modalSubmit"
                @on-cancel="this.modal=false">
            <div style="text-align:center">
                <p>是否确认{{modalContent}}!</p>
            </div>
        </Modal>
    </div>
</template>
<script>
import { findPageList, updateDeptCommitStatus, updateDeptRejectStatus, updateDeptAuditStatus, updateLeaderAuditStatus } from '@/api/outbreak-management/material-security/material-production'
export default {
  // 物资生产
  data () {
    return {
      modal: false,
      modalContent: '',
      reportStatus: '',
      isReport: false,
      id: '',
      columns: [
        {
          title: '上报日期',
          key: 'imp_date',
          minWidth: 120,
          render: (h, params) => {
            let date = this.$common.moment(params.row.imp_date).format('YYYY-MM-DD')
            return h('div', date)
          }
        },
        {
          title: '药品名称',
          minWidth: 120,
          key: 'name'
        },
        {
          title: '厂家名称',
          minWidth: 120,
          key: 'manufactor'
        },
        {
          title: '当天产量',
          minWidth: 120,
          key: 'daily_output'
        },
        {
          title: '当天库存',
          minWidth: 120,
          key: 'stock'
        },
        {
          title: '当天购买',
          minWidth: 120,
          key: 'daily_output'
        },
        {
          title: '当天需求',
          minWidth: 120,
          key: 'need'
        },
        {
          title: '提报预警',
          minWidth: 120,
          key: 'warning_status',
          render: (h, params) => {
            switch (params.row.warning_status) {
              case 'normal':
                return h('span', { style: { color: '#2db7f5' } }, '已提报')
              case 'overtime' :
                return h('span', { style: { color: '#ed4014' } }, '超时未提报')
              case 'notset':
                return h('span', { style: { color: '#ff9900' } }, '预警未设置')
              case 'ready':
                return h('span', { style: { color: '#19be6b' } }, '待提报')
            }
          }
        },
        {
          title: '审核状态',
          minWidth: 120,
          key: 'dept_audit_status',
          render: (h, params) => {
            let date = '未知状态'
            let dept_audit_status = params.row.dept_audit_status
            let leader_audit_status = params.row.leader_audit_status
            if (dept_audit_status === 'draft') {
              date = '未提报'
            } else if (dept_audit_status === 'commit') {
              date = '部门已审核'
            } else if (dept_audit_status === 'audited' && leader_audit_status === 'unaudited') {
              date = '分项已审核'
            } else if (dept_audit_status === 'audited' && leader_audit_status === 'audited') {
              date = '区已审核'
            }
            return h('div', date)
          }
        },
        {
          title: '操作',
          slot: 'action',
          fixed: 'right',
          minWidth: 180,
          align: 'center'
        }
      ],
      data: [],
      pageInfo: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  mounted () {
    this.changePage(this.pageInfo.pageNo)
  },
  methods: {
    toDetail (imp_date) {
      this.$router.push({ name: 'material-production-add', query: { imp_date: this.$common.moment(imp_date).format('YYYY-MM-DD'), showDetail: true } })
    },
    // 打开弹窗
    openModal (id, isReport, modalContent, reportStatus) {
      if (id) {
        this.isReport = isReport
        this.modalContent = modalContent
        this.reportStatus = reportStatus
        this.modal = true
        this.id = id
      }
    },
    modalSubmit () {
      if (this.isReport) {
        this.reportData()
      } else {
        this.rejectData()
      }
    },
    rejectData () {
      updateDeptRejectStatus({ id: this.id }).then(({ data }) => {
        let { errcode } = data
        if (errcode === 0) {
          this.$Message.success(this.modalContent + '成功!')
          // 清空数据
          this.id = ''
          this.handleSearch()
        } else {
          this.$Message.warning(this.modalContent + '失败!')
        }
      })
    },
    // 提报数据
    reportData () {
      if (this.reportStatus === 'commit') {
        updateDeptAuditStatus({ id: this.id }).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success(this.modalContent + '成功!')
            // 清空数据
            this.id = ''
            this.handleSearch()
          } else {
            this.$Message.warning(this.modalContent + '失败!')
          }
        })
      } else if (this.reportStatus === 'audited') {
        updateLeaderAuditStatus({ id: this.id }).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success(this.modalContent + '成功!')
            // 清空数据
            this.id = ''
            this.handleSearch()
          } else {
            this.$Message.warning(this.modalContent + '失败!')
          }
        })
      } else if (this.reportStatus === 'draft') {
        updateDeptCommitStatus({ id: this.id }).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success(this.modalContent + '成功!')
            // 清空数据
            this.id = ''
            this.handleSearch()
          } else {
            this.$Message.warning(this.modalContent + '失败!')
          }
        })
      }
    },
    // 分页查询查找
    handleSearch () {
      findPageList(this.pageInfo).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          this.data = res.results
          this.pageInfo.total = res.totalRecord
        } else {
          this.$Message.warning('获取信息失败!')
        }
      })
    },
    // 分页
    changePage (pageNo) {
      this.pageInfo.pageNo = pageNo
      this.handleSearch()
    }
  }
}
</script>

<style scoped lang="less">
</style>
